package com.woniuxy.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.entity.Client;
import com.woniuxy.entity.Salesman;
import com.woniuxy.service.SalesmanService;
import com.woniuxy.utils.Result;

@Controller
@RequestMapping("/salesman")
public class SalesmanController {

	@Autowired
	private SalesmanService salesmanService;

	
	@ResponseBody
	@RequestMapping("/all")
	public List<Salesman> findCliends() {
		return salesmanService.findAllSalesmans();
	}

	
	@ResponseBody
	@RequestMapping("/addsalesman")
	public Result<Object> addSalesman(Salesman salesman) {
		Result<Object> result = new Result<>();
		salesman.setAddtime(new Date());
		int addResult = salesmanService.addSalesman(salesman);

		if (addResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("业务员新增成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("业务员新增失败");
		}
		return result;
	}

	
	// 通过sid查询当前行的数据
	@ResponseBody
	@RequestMapping("/searchData")
	public Salesman searchData(int sid) {
		return salesmanService.findSalesmanBySid(sid);
	}

	
	// 修改当前数据
	@ResponseBody
	@RequestMapping("/modifySalesman")
	public Result<Object> searchData(Salesman salesman) {
		System.out.println(salesman.toString());

		Result<Object> result = new Result<>();
		salesman.setAddtime(new Date());
		int newResult = salesmanService.modifySalesman(salesman);
		if (newResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("数据修改成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("数据修改失败");
		}
		return result;
	}

	
	// 通过sid删除当前数据
	@ResponseBody
	@RequestMapping("/deleteSalesman")
	public Result<Object> deleteSalesman(int sid) {
		Result<Object> result = new Result<>();
		int delResult = salesmanService.deleteDataBySid(sid);
		if (delResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("数据删除成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("数据删除成功");
		}
		return result;
	}
}
